<?php

namespace App\Http\Controllers\Home;

use App\Models\Goods;
use App\Types;
use Illuminate\Http\Request;

//使用类
use App\Http\Controllers\Controller;


//分类首页控制器
class TypesController extends Controller
{
    //分类首页
	public function index($id){
	    //查询所有顶级分类
        $types= Types::where("pid",0)->get();
       // dd($types);

        //查询当前分类
        $type = Types::where("id",$id)->first();

        //将path路径处理成数组
        $arr = explode(',',$type->path);

        //dd($arr);
        $size= count($arr)-1; //几级分类
        switch($size){
            case '1':
                $erClass=Types::where([["path","like","%,".$id.",%"],['pid','!=',$id]])->get();//分类
               // dd($erClass);
                $newArr=array();
                foreach ($erClass as $key => $value){
                    $newArr[]=$value->id; //都放入数组
                }
                //dd($newArr);
                $goods=Goods::whereIn("cid",$newArr)->get();//父类包含子类内容  分类
                break;
            case '2':
                $goodsClass=Types::where("pid",$id)->get();
               // dd($goodsClass);
                $newArr=array();
                foreach ($goodsClass as $key => $value){
                    $newArr[]=$value->id; //都放入数组
                }
                //dd($newArr);
                $goods=Goods::whereIn("cid",$newArr)->get();//父类包含子类内容

                break;
            case '3':
                $goods=Goods::where("cid",$id)->get();
                break;
        }
        //dd($goods);




        //顶级分类
        $ding=$arr['1']?$arr['1']:$id;//GET   地址栏上传递的$id

		//加载页面

        //格式化数据
        $data = array(
            "types"=>$types,
            "ding"=>$ding,
            "goods"=>$goods,
        );

        return view("home.types")->with($data);
	}
	
}
